package org.example;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableEurekaClient
//@EnableHystrix
@RestController
@EnableCircuitBreaker  // 启用 Hystrix
public class Main {


    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/gradeList")
    @HystrixCommand(fallbackMethod = "fallbackMethod")
    public String getClassInfo() {
        try {
            return restTemplate.getForObject("http://class-service/grade/gradeList", String.class);
        }
        catch (Exception e){
            e.printStackTrace();
            return "消费端出现异常";
        }
    }

    public String fallbackMethod() {
        return "班级提供端未启动，hystrix断开服务";
    }

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    public static void main(String[] args) {
        SpringApplication.run(Main.class,args);
    }
}